package com.example.administrator.xiaoxiang.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.administrator.xiaoxiang.R;

import java.util.ArrayList;
import java.util.List;

public class GudeActivity extends Activity {
    private List<ImageView> list;
    private ViewPager mViewPager;
    private Button mButton;
    private ImageView[] icons;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gude);
        mViewPager= (ViewPager) findViewById(R.id.welcome_pager);
        mButton= (Button) findViewById(R.id.welcome_guide_btn);
        iniViewPager();
        initDot();
    }

    private void initDot() {
        icons=new ImageView[4];
        LinearLayout layout= (LinearLayout) findViewById(R.id.layout);
        for (int i=0;i<icons.length;i++){
            icons[i]= (ImageView) layout.getChildAt(i);
            icons[i].setEnabled(true);

        }
        icons[0].setEnabled(false);
    }

    private void iniViewPager() {

        list=new ArrayList<>() ;
        ImageView iv1=new ImageView(this);
        iv1.setBackgroundResource(R.mipmap.guide_1);
        list.add(iv1);
        ImageView iv2=new ImageView(this);
        iv2.setBackgroundResource(R.mipmap.guide_2);
        list.add(iv2);
        ImageView iv3=new ImageView(this);
        iv3.setBackgroundResource(R.mipmap.guide_3);
        list.add(iv3);
        ImageView iv4=new ImageView(this);
        iv4.setBackgroundResource(R.mipmap.guide_4);

        list.add(iv4);
        mViewPager.setAdapter(new MyPagerAdapter());
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < icons.length; i++) {
                    icons[i].setEnabled(true);
                }
                icons[position].setEnabled(false);
                if(position==3){
                    mButton.setVisibility(View.VISIBLE);
                }else {
                    mButton.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void onClick(View view) {


        startActivity(new Intent(this,XiaoGuoActivity.class));
        finish();
    }
    class MyPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //  super.destroyItem(container, position, object);
            container.removeView(list.get(position));
        }
    }
}
